import QtQuick 1.1
import com.nokia.meego 1.0

PageTemplate {
    id: filesPage

    property variant type: null;
    //property variant fileEdit: null;

    CaptionItem {
        id: captionItem
        Row {
            id: fileRow
            parent: captionItem.captionContentRow
            width: parent.width
            Button {
                id: fileUpButton
                iconSource: "image://theme/icon-m-toolbar-up" + (theme.inverted ? "-white" : "")
                width: height * 1.3
                onClicked: {
                    var idx = filesListView.model.parentModelIndex();
                    filesListView.model.rootIndex = idx;
                }
            }
            TextField {
                id: fileNameEdit
                width: fileRow.width - applyButton.width - fileUpButton.width
            }
            Button {
                id: applyButton
                text: ""
                onClicked: {
                    filesPage.fileApply();
                }
            }
        }
    }

    FileSystemModel {
        id: filesVisualModel
        Component.onCompleted: {
            var directory = appWindow.currentFilePath();
            if (directory !== null) {
                directory = filesVisualModel.model.directoryFromPath(directory);
                if (!filesVisualModel.model.dirExists(directory))
                    directory = null;
            }
            if (directory === null || directory.trim().length < 1) {
                directory = filesVisualModel.model.homePath();
                if (filesVisualModel.model.dirExists(directory + 'MyDocs/'))
                    directory += 'MyDocs/';
            }
            filesVisualModel.setView(filesListView, directory);
        }
    }

    InfoItemView {
        id: filesListView
        y: captionItem.height
        width: filesPage.width
        height: filesPage.height - captionItem.height
        model: filesVisualModel
        //section.property: "section"
    }

    ScrollDecorator {
        flickableItem: filesListView
    }

    function fileClicked(filePath) {
        fileNameEdit.text = filePath;
    }

    function fileApply() {
        var f = fileNameEdit.text;
        console.log("fileApply type=" + filesPage.type + " file=" + f);
        if (filesPage.type === 'new') {
            if (appWindow.editor.newDocument(f))
                appWindow.showEditorPage();
        }
        else if (filesPage.type === 'open') {
            if (appWindow.editor.loadDocument(f))
                appWindow.showEditorPage();
        }
        else if (filesPage.type === 'save') {
            if (appWindow.editor.saveDocument(f))
                appWindow.showEditorPage();
        }
    }

    function setType(type) {
        if (type !== 'new' && type !== 'open' && type !== 'save')
            throw 'Unhandled FileSystemPage type=' + type;

        filesVisualModel.fileClicked.connect(filesPage.fileClicked);

        filesPage.type = type;
        if (type === 'new')
            applyButton.text = qsTr('Create');
        else if (type === 'open')
            applyButton.text = qsTr('Open');
        else if (type === 'save')
            applyButton.text = qsTr('Save');

        var path = appWindow.currentFilePath();
        var pathUndefined = (path === null || path === undefined || path.trim().length < 1 || path.indexOf('[') === 0);
        if (pathUndefined) {
            path = filesVisualModel.model.homePath();
            if (filesVisualModel.model.dirExists(path + 'MyDocs/'))
                path += 'MyDocs/';
        }
        /*
        if (type === 'new') {
            var num = 1;
            var p = null;
            do {
                p = filesVisualModel.model.pathFromDirFile(path,'default' + num + '.txt');
                if (!filesVisualModel.model.fileExists(p))
                    break;
                num += 1;
            } while(true);
            if (p !== null && p !== undefined)
                path = p;
        }
        */
        fileNameEdit.text = (path !== null && path !== undefined ? path : '');
    }

    Component.onCompleted: {
        // set the minimum button width to still have the text on it fully visible
        var button = Qt.createQmlObject('import QtQuick 1.1; import com.nokia.meego 1.0; Button { text:"" }', fileRow);
        var code = 'import QtQuick 1.1; Text { font.pixelSize:' + button.font.pixelSize + '; font.bold:' + button.font.bold;
        if (button.font.weight == Font.Light) code += ';font.weight:"Light"'
        else if (button.font.weight == Font.Normal) code += ';font.weight:"Normal"'
        else if (button.font.weight == Font.DemiBold) code += ';font.weight:"DemiBold"'
        else if (button.font.weight == Font.Bold) code += ';font.weight:"Bold"'
        else if (button.font.weight == Font.Black) code += ';font.weight:"Black"'
        var text1 = Qt.createQmlObject(code + '; text:"' + qsTr("Create") + '"}', fileRow, "calcTextWidth");
        var text2 = Qt.createQmlObject(code + '; text:"' + qsTr("Open") + '"}', fileRow, "calcTextWidth");
        var text3 = Qt.createQmlObject(code + '; text:"' + qsTr("Save") + '"}', fileRow, "calcTextWidth");
        var spacing = 10;
        applyButton.width = Math.max(text1.width, Math.max(text2.width, text3.width)) + spacing*2;
        button.destroy();
        text1.destroy();
        text2.destroy();
        text3.destroy();
    }

    Component.onDestruction: {
        console.log("FileSystemPage.onDestruction")
    }
}
